<?php 
/**
 *  跑车商争议数据
 */
date_default_timezone_set("Asia/Shanghai");

set_time_limit(0);

if (isset ( $_SERVER ['REQUEST_URI'] )) {
    die ( 'error' );
}
if (! defined ( 'API_ROOT_PATH' )) {
    define ( 'API_ROOT_PATH', dirname ( __FILE__ ) );
}

require_once (API_ROOT_PATH . '/../inc/Config.php');
require_once (API_ROOT_PATH . '/../inc/dblink.php');
require_once (API_ROOT_PATH . '/../inc/Mysql.php');

$plate_prefix = array(
		     	'1' => '京',
		        '2' => '冀',
		        '3' => '津',
		        '4' => '晋',
		        '5' => '蒙',
		        '6' => '辽',
		        '7' => '吉',
		        '8' => '黑',
		        '9' => '豫',
		        '10' => '鲁',
		        '11' => '苏',
		        '12' => '渝',
		        '13' => '粤',
		        '14' => '浙',
		        '15' => '贵',
		        '16' => '川',
		        '17' => '闽',
		        '18' => '皖',
		        '19' => '桂',
		        '20' => '琼',
		        '21' => '宁',
		        '22' => '沪',
		        '23' => '甘',
		        '24' => '陕',
		        '25' => '青',
		        '26' => '鄂',
		        '27' => '湘',
		        '28' => '赣',
		        '29' => '云',
		        '30' => '新',
		        '31' => '藏',
		        '32' => '港',
		        '33' => '澳'
		    );

$db = new Mysql ( MYSQLDB, MYSQLDB_USER, MYSQLDB_PWD, MYSQLDB_DATABASE );

//找到未处理的争议数据
$sql = "select * from au_dealer_issue where status = 0";

$dis_data = $db ->fetchAll($sql);

foreach ($dis_data as $key => $value) {
	if (in_array($value['issue_type'], [1,2,4,6,7])) {

		$res = addDispute($value['order_id'],$value['dealer_id'],1,$value['createtime']);

	}elseif (in_array($value['issue_type'], [3,5])) {

		$res = addDispute($value['order_id'],$value['dealer_id'],2,$value['createtime']);

	}
	
}

//添加争议记录
function addDispute($order_id,$dealer_id,$dispute_reason,$createtime){

	global $db;

	global $plate_prefix;

	$order_info_sql = "select * from au_order where order_id = ".$order_id;

	$order_info = $db ->fetchOne($order_info_sql);

	$purchase_order_sql = "select * from au_purchase_order where order_id = ".$order_id;

	$purchase_order_info = $db ->fetchOne($purchase_order_sql);

	$car_sql = "select * from au_cars where car_id = ".$order_info['car_id'];

	$car_info = $db ->fetchOne($car_sql);

	$car_plate_number = $plate_prefix[$car_info['plate_prefix']].$car_info['plate_suffix'];

	$d_sql = "insert into au_dispute (`order_id`,`order_no`,`order_status`,`po_id`,`po_no`,`car_plate_number`,`od_id`,`od_type`,`dispute_reason`,`status`,`createtime`)
	 values
	('".$order_info['order_id']."','".$order_info['order_no']."','".$order_info['status']."','".$purchase_order_info['id']."','".$purchase_order_info['purchase_order_nu']."','".$car_plate_number."','".$dealer_id."',1,'".$dispute_reason."',1,'".$createtime."')";

	$ok = $db ->query($d_sql);

	$okNun = $db ->affectedRows();

	if ($okNun > 0){
		print_r("争议生成成功\n");
		$res_lock = lockAll($order_id,$purchase_order_info['id']);
	}else{
		print_r("争议生成失败\n");
	}
}
//添加变更单记录
// function addChangeOrder($order_id,$source){

// 	global $db;

// 	global $plate_prefix;

// 	$order_info_sql = "select * from au_order where order_id = ".$order_id;

// 	$order_info = $db ->fetchOne($order_info_sql);

// 	$purchase_order_sql = "select * from au_purchase_order where order_id = ".$order_id;

// 	$purchase_order_info = $db ->fetchOne($purchase_order_sql);

// 	$car_sql = "select * from au_cars where car_id = ".$order_info['car_id'];

// 	$car_info = $db ->fetchOne($car_sql);

// 	$car_plate_number = $plate_prefix[$car_info['plate_prefix']].$car_info['plate_suffix'];

// 	$modify_no = genBillsNo('BG', '01');

// 	$m_sql = "insert into au_modify_list 
// 	(`modify_no`,`order_id`,`order_no`,`order_status`,`po_id`,`po_no`,`car_plate_number`,`source`,`status`,`createtime`)
// 	 values
// 	('".$modify_no."','".$order_info['order_id']."','".$order_info['order_no']."','".$order_info['status']."','".$purchase_order_info['id']."','".$purchase_order_info['purchase_order_nu']."','".$car_plate_number."','".$source."',0,now())";
// 	$ok = $db ->query($m_sql);
// 	if ($ok > 0){
		
// 		print_r("变更单生成成功\n");

// 		 $res_changeOrder_log_sql = "insert into  au_modifylist_operation_log (`mid`,`desc`,`createtime`) values ('".$ok."',1,now())";

// 	    $ok1 = $db ->query($res_changeOrder_log_sql);

// 		$res_lock = lockAll($order_id,$purchase_order_info['id']);
// 	} 
		
// 	else{
// 		print_r("变更单生成失败\n");
// 	}

   
// };

//
function lockAll($order_id,$po_id){
	global $db;
	//锁拍单
	$order_sql = "update au_order set dealer_issue_status = 1 ,is_lock = 1 where order_id = ".$order_id;

	$res_order = $db ->query($order_sql);

	$ro = $db ->affectedRows();

	if ($ro > 0) 
		print_r("拍单锁定成功\n");
	else
		print_r("拍单锁定失败\n");
	//锁采购单
	$update_purchase_order_sql = "update au_purchase_order set is_lock = 1 where id = ".$po_id;

	$res_purchase_order = $db ->query($update_purchase_order_sql);

	$rp = $db ->affectedRows();

	if ($rp > 0) 
		print_r("采购单锁定成功\n");
	else
		print_r("采购单锁定失败\n");
	//锁定收款单
	$collection_sql = "update au_order_collections as ac,au_collection as c set c.status=2 where ac.collection_id = c.id and c.payment_type in (1,2,3) and ac.order_id = ".$order_id;

	$res_collection = $db ->query($collection_sql);

	$rc = $db ->affectedRows();

	if ($rc > 0) 
		print_r("收款单锁定成功\n");
	else
		print_r("收款单锁定失败\n");
	//锁定付款单
	$payment_sql = "update au_purchase_payments as pp,au_payment as p set p.status=2 where pp.payment_id = p.id and p.pay_type in (1,2) and pp.po_id = ".$po_id;

	$res_payment = $db ->query($payment_sql);

	$rpa = $db ->affectedRows();

	if ($rpa > 0) 
		print_r("付款单锁定成功\n");
	else
		print_r("付款单锁定失败\n");
};

$db->close();

unset($db);  

$db = null;

// /**
//  * 产生固定长度的随机字符串
//  * @param $len
//  * @param null $chars
//  * @return string
//  */
// function genRandomChars($len, $chars = null)
// {
//     if (is_null($chars)) {
//         $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//     }
//     mt_srand(10000000 * (double)microtime());
//     for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
//         $str .= $chars[mt_rand(0, $lc)];
//     }
//     return $str;
// }

// /**
//  * 产生一个四位的随机字符串
//  * @return string
//  */
// function gen4Chars()
// {
//     $strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
//     $numbers = "0123456789";
//     $randomString = genRandomChars(3);
//     if (ctype_digit($randomString)) {
//         $randomString = $randomString . genRandomChars(1, $strChars);
//     } elseif (ctype_alpha($randomString)) {
//         $randomString = $randomString . genRandomChars(1, $numbers);
//     } else {
//         $randomString = $randomString . genRandomChars(1);
//     }
//     return $randomString;
// }

// /**
//  * 产生一个单据编号
//  * @param $type
//  * @param $subType
//  * @return string
//  */
// function genBillsNo($type, $subType)
// {
//     if ($subType < 10) $subType = "0" . $subType;
//     return $type . $subType . date("Ymd") . gen4Chars();
// }
